package org.lzy.shop.feedback

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnRefreshListener
import org.lzy.shop.Constant
import org.lzy.shop.R
import org.lzy.shop.adapter.FeedBackAdapter
import org.lzy.shop.base.BaseMvvmActivity
import org.lzy.shop.databinding.ActivityFeedbackBinding
import org.lzy.shop.viewmodel.FeedBackModel

/**
 * 反馈
 */
class FeedBackListActivity : BaseMvvmActivity<ActivityFeedbackBinding, FeedBackModel>({ ActivityFeedbackBinding.inflate(it) }),
    OnRefreshListener {
    var goodsID : String?=null
    var mPageIndex = 1
    var feedBackAdapter: FeedBackAdapter?=null

    // 标记是否正在加载更多
    private var isLoadingMore = false
    // 标记是否没有更多数据
    private var isNoMoreData = false

    override fun initView() {
        if (intent.extras != null) {
            val bundle = intent.extras
            goodsID = bundle!!.getString(Constant.GOODID)
        }
        setTitleText(getResString(R.string.feedback))
        feedBackAdapter = FeedBackAdapter()
        val layoutManager = LinearLayoutManager(this@FeedBackListActivity)
        binding.recyclerview.apply {
            this.layoutManager = layoutManager
            adapter = feedBackAdapter
            // 添加滚动监听器实现上拉加载更多
            addOnScrollListener(object : RecyclerView.OnScrollListener() {
                override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                    super.onScrolled(recyclerView, dx, dy)
                    // 只有向下滚动且没有在加载更多且还有数据时才处理
                    if (dy > 0 && !isLoadingMore && !isNoMoreData) {
                        val lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition()
                        val totalItemCount = layoutManager.itemCount
                        // 当滑动到最后一个可见项时，触发加载更多
                        if (lastVisibleItemPosition >= totalItemCount - 1) {
                            loadMoreData()
                        }
                    }
                }
            })
        }
        binding.refreshLayout.setOnRefreshListener(this)
    }

    override fun initData() {
        mViewModel.getFeedBackTypeData.observeWithLifecycle(this){feedbackTypes ->
            feedbackTypes?.let {

            }
        }
        mViewModel.getFeedBackData.observeWithLifecycle(this){feedBackResponse ->
            finishRefresh()
            feedBackResponse?.let {
                if (feedBackResponse.isNotEmpty()) {
                    if (mPageIndex == 1) {
                        feedBackAdapter?.submitList(feedBackResponse)
                    } else {
                        feedBackAdapter?.addAll(feedBackResponse)
                    }
                } else {
                    if (mPageIndex > 1) {
                        isNoMoreData = true
                    } else {
                        showListEmpty()
                    }
                }
                // 完成加载
                isLoadingMore = false

            }
        }
    }

    override fun requestData() {
        getFeedBack()

    }



    //反馈列表
    fun getFeedBack(){
        mViewModel.getFeedBack(mPageIndex)

    }

    override fun allClick() {
        //添加反馈
        binding.feedNews.setOnClickListener {
            startActivity(FeedBackSubmitActivity::class.java)
        }
    }

    fun showListEmpty() {
        feedBackAdapter?.submitList(null)
        feedBackAdapter?.isStateViewEnable = true
        feedBackAdapter?.stateView = showEmptyView()
    }

    private fun finishRefresh() {
        // 停止刷新
        binding.refreshLayout.isRefreshing = false
    }

    private fun loadMoreData() {
        if (!isLoadingMore) {
            isLoadingMore = true
            mPageIndex++
            getFeedBack()
        }
    }

    override fun onRefresh() {
        mPageIndex = 1
        isNoMoreData = false
        getFeedBack()
    }

}